package com.otp.Activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.otp.customUI.NavigationView;
import com.otp.customUI.TabMenu;
import com.otp.fragment.ProfileFragment;
import com.otp.fragment.VideoFragment;
import com.otp.util.HttpUtil;
import com.otp.util.UploadFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class DashBoard extends BaseActivity implements View.OnClickListener{
    private NavigationView titleBar = null;
    private TabMenu videoTabMenu = null;
    private TabMenu userTabMenu = null;
    private int currentVisiable = 0;
    public static boolean isForeground = false;
    public static boolean isExit = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);
        videoTabMenu = (TabMenu) this.findViewById(R.id.videoTabMenu);
        videoTabMenu.setOnClickListener(this);
        userTabMenu = (TabMenu) this.findViewById(R.id.userTabMenu);
        titleBar = (NavigationView) findViewById(R.id.titlebar);
        userTabMenu.setOnClickListener(this);
        VideoFragment videoFragment = new VideoFragment();
        FragmentTransaction transaction=getFragmentManager().beginTransaction();
        transaction.replace(R.id.video, videoFragment);
        transaction.commit();
        titleBar.setTitle("视频");
        init();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        } else if(keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
        }
    }

    // 初始化 JPush。如果已经初始化，但没有登录成功，则执行重新登录。
    private void init(){
        JPushInterface.setDebugMode(true);
        JPushInterface.init(getApplicationContext());
    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        FragmentManager manager=getFragmentManager();
        //获得事物的操作
        FragmentTransaction transaction=manager.beginTransaction();

        switch (v.getId()){
            case R.id.videoTabMenu:
                if (currentVisiable != R.id.videoTabMenu ) {
                    currentVisiable = R.id.videoTabMenu;
                    VideoFragment videoFragment = new VideoFragment();
                    transaction.replace(R.id.video, videoFragment);
                    transaction.commit();
                    titleBar.setTitle("课程");
                    videoTabMenu.setTopTextColor(this.getResources().getColor(R.color.colorPrimary));
                }
                break;
            case R.id.userTabMenu:
                if (currentVisiable != R.id.userTabMenu) {
                    currentVisiable = R.id.userTabMenu;
                    ProfileFragment myFragment = new ProfileFragment();
                    transaction.replace(R.id.video, myFragment);
                    transaction.commit();
                    titleBar.setTitle("我的");
                    userTabMenu.setTopTextColor(this.getResources().getColor(R.color.colorPrimary));
                    videoTabMenu.setTopTextColor(this.getResources().getColor(R.color.colorBlack));
                }
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
/*        menu.add(Menu.NONE, Menu.FIRST + 1, 1, "从相册选取");
        menu.add(Menu.NONE, Menu.FIRST + 2, 2, "用相机拍照");*/
        getMenuInflater().inflate(R.menu.avatar_menu, menu);
        return true;
    }



}
